<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/article_description/to/article_description_to.php");
require_once("model/article_description/dao/article_description_dao_factory.php");

require_once("model/article/to/article_to.php");
require_once("model/article/dao/article_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");


class Article_description_dao_test {

	private $dao;
	private $autor;
	private $article;
	
	public function __construct() {}
	
	public function before_test() {		
		try {	
			$this->autor = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
			$date = date("Y-m-d H:i:s");
			$this->article = (object) Article_dao_factory::singleton()->get_dao()->create(new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0));
			
			$this->dao = Article_description_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			$e->getMessage() . '</br>';
		}
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_description_to = new Article_description_to(0, 'content', $this->article->get_number(), 
									$this->autor->get_login(), $date, 'open');
			
			//create
			$article_description_to = $this->dao->create($article_description_to);	
			echo 'create article_description: ' . $article_description_to->to_string() . '<br/>';
			
			//Create description 2
			$article_description_to2 = new Article_description_to(0, 'content2', $this->article->get_number(), 
									$this->autor->get_login(), $date, 'open');
			$article_description_to2 = $this->dao->create($article_description_to2);
			
			//Create existente
			try {
				$this->dao->create($article_description_to2);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}

			//exist inexistent 
			if (!$this->dao->exist(9999))
				echo 'No exist description 9999<br/>';
			else 
				throw new Exception("Error, exist description inexistent");
			
			//exist existent 
			$this->dao->exist($article_description_to->get_number());
			echo 'exist article_description: ' . $article_description_to->to_string() . '<br/>';

			//find inexistent 
			try {
				$this->dao->find_by_description_number(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find existent 
			$aux_article_description_to = $this->dao->find_by_description_number($article_description_to->get_number());	
				if ($article_description_to->equals($aux_article_description_to)) 
					echo 'found article_description: ' . $article_description_to->to_string() . '<br/>';
				else
					echo 'not equal article_description inserted';
					
			//find by article inexistent
			try {
				$article_description_list = $this->dao->find_by_article_number(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
				
			//find descriptions by article existent
			$article_description_list = $this->dao->find_by_article_number($this->article->get_number());
				echo 'Find descriptions for article: ' . $this->article->to_string() . '</br>';
				foreach ($article_description_list as $description_to)
					echo '    description: ' . $description_to->to_string() . '</br>';
			
			//find last by article inexistent
			try {
				$description = $this->dao->find_last_by_article_number(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find last by article existent
			$description = $this->dao->find_last_by_article_number($this->article->get_number());
				echo 'Last description for article: ' . $description->to_string() . '</br>';
			
			//count descriptions by autor
			$descriptions = $this->dao->count_descriptions_by_login($this->autor->get_login());
			echo 'Count descriptions by autor: ' . $descriptions . '</br>';
				
			//update 
			$article_description_to->set_state('wait');
			$this->dao->update($article_description_to);
			$aux_article_ping_to = $this->dao->find_by_description_number($article_description_to->get_number());	
			echo 'update article_description: ' . $aux_article_ping_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//remove existent 
			$this->dao->remove($article_description_to->get_number());
			echo 'remove article_description: ' . $article_description_to->to_string() . '<br/>';
				
		}
		catch (Duplicate_instance_exception $e) {
			echo $e->getMessage();
		}
		catch (Internal_error_exception $e) {
			echo $e->getMessage();
		}
		catch (Instance_not_found_exception $e) {
			echo $e->getMessage();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function after_test() {
		Article_dao_factory::singleton()->get_dao()->remove($this->article->get_number());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}

//test
$test = new Article_description_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>